package cn.bonoon.kernel.web.components;

import cn.bonoon.kernel.descriptors.UIPropertyAccessor;
import cn.bonoon.kernel.web.annotations.components.AsEditor;
import cn.bonoon.kernel.web.annotations.components.AsEditor.EditorToolbar;
import cn.bonoon.kernel.web.html.UIPropertyBuilder;
import cn.bonoon.kernel.web.html.form.ComponentPropertyParser;

public class EditorComponent extends AbstractComponent{
	private final AsEditor com;
	public EditorComponent(ComponentPropertyParser property, UIPropertyAccessor accessor, AsEditor com) {
		super(property, accessor);
		this.com=com;
	}

	@Override
	protected void parser(UIPropertyBuilder builder) {
		componentParser.hasFile();
		StringBuilder html = builder.getInitHtml();
		int width = componentWidth();
		html.append("<textarea");
		__component(html);
		html.append("style='width:").append(width);
		html.append("px;height:").append(com.height());
		html.append("px;float:left;'>$!{").append(builder.getBindedName());
		html.append('.').append(componentName).append("}</textarea>");
		
		builder.getInitBlock()
			.append("KindEditor.create('#")
			.append(componentId)
			.append("',{basePath:'")
			.append(builder.path("/res/editor/"))
			.append("',uploadJson : '")
			.append(builder.path("/upload_json.jsp"))
			.append("',fileManagerJson : '")
			.append(builder.path("/file_manager_json.jsp"))
			.append("',allowFileManager:true,minWidth:600,minHeight:50,items:[");
		String[] items = com.value();
		if(items.length > 0){
			for (int i = 0; i < items.length; i++) {
				String item = items[i];
				if(i > 0){
					builder.getInitBlock().append(',');
				}
				builder.getInitBlock().append('\'').append(item).append('\'');
			}
		}else{ 
			String ss;
			if(com.toolbar() == EditorToolbar.STANDARD){
				ss = "'source','|','undo','redo','|','preview','print','cut','copy','paste','plainpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'indent', 'outdent', 'clearhtml', 'quickformat','selectall', '|', '/','formatblock','fontname', 'fontsize', '|', 'forecolor','hilitecolor', 'bold','italic', 'underline','strikethrough','lineheight', 'removeformat'";
			}else if(com.toolbar() == EditorToolbar.SIMPLE){
				ss = "'undo','redo','|','copy','paste','clearhtml', '|', 'formatblock','fontname', 'fontsize', '|', 'forecolor','hilitecolor', 'bold','italic', 'underline','strikethrough','lineheight'";
			}else{
				ss = "'source','|','undo','redo','|','preview','print','template','cut','copy','paste','plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist','insertunorderedlist', 'indent', 'outdent','subscript','superscript', 'clearhtml', 'quickformat','selectall', '|', '/','formatblock','fontname', 'fontsize', '|', 'forecolor','hilitecolor', 'bold','italic', 'underline','strikethrough','lineheight', 'removeformat','image'";
			}
			builder.getInitBlock().append(ss);
		}
		builder.getInitBlock().append("]});");
		
		builder.getInitSubmit().append("KindEditor.sync('#").append(componentId).append("');");
		
		if(!builder.hasSameType(AsEditor.class)){//判断下是否已经使用过该注解的情况
			builder.js("/res/editor/kindeditor-min.js");
			builder.js("/res/editor/lang/zh_CN.js");
		}
	}
}
